package com.wc.alorithm_blue_bridge._日期问题.Q1562;

import java.io.*;

/**
 * @Author congege
 * @Description
 * 完全日期
 * http://43.138.190.70:8888/p
 * @Date Created in 2023/12/4-10:17
 */
public class Main {
    static int[] months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

    public static void main(String[] args) {
        // 1 ~ 9999年每一年的完全平方数的个数
        int[] perfects = new int[(int) 1e4];
        // 前缀和
        int[] pre = new int[(int) 1e4];

        for (int i = 1; i < perfects.length; i++) {
            perfects[i] = calPerfectNum(i);
            pre[i] = pre[i - 1] + perfects[i];
        }

        int n = Integer.parseInt(nextLine());
        while (n-- > 0) {
            String[] yearStr = nextLine().split(" ");
            int yearStart = Integer.parseInt(yearStr[0]);
            int yearEnd = Integer.parseInt(yearStr[1]);
            out.println(pre[yearEnd] - pre[yearStart - 1]);
        }

        out.flush();
    }

    public static String nextLine() {
        String s = null;
        try {
            s = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }


    /**
     * 计算当年的完全平方数个数
     *
     * @param year 年份
     * @return
     */
    public static int calPerfectNum(int year) {
        //
        int ans = 0;
        int yearY = year;
        int yearNum = 0;

        if (isRun(year)) {
            months[2] = 29;
        } else {
            months[2] = 28;
        }

        while (year != 0) {
            yearNum += year % 10;
            year /= 10;
        }
        for (int i = 1; i <= 12; i++) {
            int month = i;
            int monthNum = 0;
            while (month != 0) {
                monthNum += month % 10;
                month /= 10;
            }
            for (int j = 1; j <= months[i]; j++) {
                int day = j;
                int dayNum = 0;
                while (day != 0) {
                    dayNum += day % 10;
                    day /= 10;
                }
                int sum = yearNum + monthNum + dayNum;
                if (isPerfect(sum)) {
                    ans++;
                }
            }
        }
        return ans;
    }

    /**
     * 是否为闰年
     *
     * @param year 年份
     * @return
     */
    public static boolean isRun(int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    /**
     * 是否为完全平凡数
     * @param num
     * @return
     */
    public static boolean isPerfect(int num) {
        int sqrt = (int) Math.sqrt(num);
        return sqrt * sqrt == num;
    }
}
